import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import $axios from '../tools/axios'

const basepath = '/orders'
export const useOrdersStore = defineStore('orders', {
  state: () => {
    return {
      allorders: [],
      money: 0,
      oid: 0,
      myTicket: []
    }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    // 获取当前时间
    getCurrentTime() {
      var now = new Date()
      var year = now.getFullYear()
      var month = now.getMonth() + 1
      var day = now.getDate()
      var hour = now.getHours()
      var minute = now.getMinutes()
      var second = now.getSeconds()

      // 格式化月、日、时、分、秒，如果小于10，在前面补0
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
      hour = hour < 10 ? '0' + hour : hour
      minute = minute < 10 ? '0' + minute : minute
      second = second < 10 ? '0' + second : second

      // 在actions中使用this.state中的属性
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
    },

    async query() {
      const path = basepath + '/query?name=' + sessionStorage.getItem('name')
      const { data: res } = await $axios.post(path)

      if (res.code === 200) {
        console.log(res.data)

        this.allorders = res.data

        this.allorders.forEach((elt) => {
          if (elt.carriage == '01') {
            elt.seattype = '商务座'
          } else if (elt.carriage == '02') {
            elt.seattype = '一等座'
          } else {
            elt.seattype = '二等座'
          }

          if (elt.state == 0) {
            elt.st = '未支付'
          } else elt.st = '已支付'
        })
      } else {
        ElMessage.error('查询订单失败！')
      }
    },

    async pay() {
      // 扣款
      const path = '/user/pay?name=' + sessionStorage.getItem('name') + '&money=' + this.money
      const { data: res } = await $axios.post(path)

      if (res.code === 200) {
        console.log(res.data)
      } else {
        ElMessage.error('扣款失败！')
      }

      // 修改订单状态/添加支付时间
      const path2 = '/orders/mod?oid=' + this.oid + '&paytime=' + this.getCurrentTime()
      const { data: res2 } = await $axios.post(path2)

      if (res2.code === 200) {
        console.log(res2.data)
      } else {
        ElMessage.error('支付失败！')
      }
    },

    async queryMyTicket() {
      const path = basepath + '/queryticket?name=' + sessionStorage.getItem('name')
      const { data: res } = await $axios.post(path)

      if (res.code === 200) {
        this.myTicket = res.data

        this.myTicket.forEach((elt) => {
          if (elt.carriage == '01') {
            elt.seattype = '商务座'
          } else if (elt.carriage == '02') {
            elt.seattype = '一等座'
          } else {
            elt.seattype = '二等座'
          }
        })
        console.log(this.myTicket)
      } else {
        ElMessage.error('查询本人车票失败！')
      }
    }
  }
})
